﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using TvControl;
using TvEngine;

namespace mediaPrime.TVServerPlugin.MediaPortal
{
    public class TVServerPlugin : ITvServerPlugin
    {
        public static IController Controller;

        private ServiceHost host;

        #region ITvServerPlugin Member

        public string Author
        {
            get { return "Sascha Lange"; }
        }

        public bool MasterOnly
        {
            get { return true; }
        }

        public string Name
        {
            get { return "mediaPrime TV Server Plugin"; }
        }

        private SettingsDialog settingsDialog;

        public SetupTv.SectionSettings Setup
        {
            get
            {
                if (settingsDialog == null)
                    settingsDialog = new SettingsDialog();

                return settingsDialog;
            }
        }

        public void Start(TvControl.IController controller)
        {
            Controller = controller;

            // To run WCF host without admin rights:
            // netsh http add urlacl url=http://+:8066/tvserver user=XXXXX
            // user = PC-Username (ggf. DOMAIN\User)

            Uri baseAddress = new Uri("http://localhost:8066/tvserver");

            // Create the ServiceHost.
            host = new ServiceHost(typeof(TVServerPluginService), baseAddress);
            {

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                host.Open();
            }
        }

        public void Stop()
        {
            if (host != null)
            {
                host.Close();
            }
        }

        public string Version
        {
            get { return "1.0.0"; }
        }

        #endregion
    }
}
